/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.gis.eemc.web.validators;

import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.FacesValidator;
import javax.faces.application.FacesMessage;

import net.gis.eemc.service.DisciplineServiceImpl;

/**
 *
 * @author Sergey
 */
@FacesValidator("disciplineCodeNumberValidator")
public class DisciplineCodeNumberValidator implements Validator {
    DisciplineServiceImpl discipServ;
    final String errMsg = "Дисциплина с таким шифром уже существует";

    /** Creates a new instance of FacultyTitleValidator */
    public DisciplineCodeNumberValidator() {
        discipServ = new DisciplineServiceImpl();
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String inputCode = value.toString();

        if (discipServ.isSpecialityWithCodeExists(inputCode)) {
            FacesMessage msg = new FacesMessage(errMsg);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }

}
